/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package datas;

import Beans.Message;
import Beans.User;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

/**

 @author Marie-Claude
 */
public interface MessageMapper {

  final String INSERT = "INSERT INTO messages (senderID, recipientID, messageText, subject) VALUES (#{senderID}, #{recipientID}, #{messageText}, #{subject})";
  final String SELECT_ALL = "select * from messages where recipientID=#{recipientID}";
  final String SELECT_BY_ID = "select * from messages where messageID=#{messageID}";

  @Insert(INSERT)
  void insertMessage(Message message);

  @Delete("delete from messages where messageID=#{messageID}")
  void delete(int messageID);

  @Select(SELECT_ALL)
  @Results(value = {
    @Result(property = "messageID", column = "messageID"),
    @Result(property = "sendingUser", column = "senderID", javaType = User.class, one = @One(select = "getUserById")),
    @Result(property = "recipientID", column = "recipientID"),
    @Result(property = "messageText", column = "messageText"),
    @Result(property = "subject", column = "subject"),
    @Result(property = "date", column = "date")
  //@Result(property = "inReplyToMsgID", column = "inReplyToMsgID")
  })
  List<Message> getAllMessages(int userId);

  @Select(SELECT_BY_ID)
  @Results(value = {
    @Result(property = "messageID", column = "messageID"),
    @Result(property = "sendingUser", column = "senderID", javaType = User.class, one = @One(select = "getUserById")),
    @Result(property = "recipientID", column = "recipientID"),
    @Result(property = "messageText", column = "messageText"),
    @Result(property = "subject", column = "subject")
  //@Result(property = "inReplyToMsgID", column = "inReplyToMsgID")
  })
  Message getMessageById(int messageID);

  @Select("select username from user where idUser=#{senderID}")
  @Results(value = {
    @Result(property = "username", column = "username"),
    @Result(property = "userID", column = "idUser")})
  User getUserById(int senderID);
}
